<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class LogMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);

        //记录接口日志
        $data = [
            'method' => $request->method(),
            'param'  => $request->all(),
            'response' => ($response&&method_exists($response, 'getData'))?$response->getData(true):'',
        ];

        if (stripos($request->url(), 'v1') !== false) {
            $channel = 'v1api';
        } else {
            $channel = 'v2api';
        }
        Log::channel($channel)->info($request->url(), $data); 

        return $response;
    }
}
